package com.tao.com.xiaowen.module.joketext;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.AppActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.R;
import com.tao.com.xiaowen.adapter.TextJokeAdapter;
import com.tao.com.xiaowen.config.ToastUtil;
import com.tao.com.xiaowen.event.UserAuthEvent;
import com.tao.com.xiaowen.event.UserInfoEvent;
import com.tao.com.xiaowen.model.JokeData;
import com.tao.com.xiaowen.module.jokecomment.JokeCommentActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2016/8/1.
 */
public class TextJokeFragment extends Fragment {

    private View view;
    private List<JokeData> jokeList;
    private XRecyclerView recyclerView;
    private TextJokeAdapter textJokeAdapter;

    private int limit = 20;
    private int skip = 20;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_text_joke, container, false);
        EventBus.getDefault().register(this);
        initView();
//        DataUntils.getJokeData(Model.JOKE_URL);
        return view;
    }

    private void initView() {
        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                BmobQuery<JokeData> query = new BmobQuery<>();
                query.order("createdAt");
                query.setLimit(limit);
                query.findObjects(new FindListener<JokeData>() {
                    @Override
                    public void done(List<JokeData> list, BmobException e) {
                        if (e == null) {
                            for (JokeData jokeData : list) {
                                if (TextUtils.isEmpty(jokeData.getPicture())) {
                                    jokeList.add(jokeData);
                                }
                            }
//                            ToastUtil.toast(getActivity(), "已经加载了最新的数据");
                            recyclerView.refreshComplete();
                            textJokeAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(getActivity(), e.getMessage());
                        }
                    }
                });

            }

            @Override
            public void onLoadMore() {
                BmobQuery<JokeData> query = new BmobQuery<>();
                query.order("createdAt");
                query.setSkip(skip);
                query.setLimit(limit);
                query.findObjects(new FindListener<JokeData>() {
                    @Override
                    public void done(List<JokeData> list, BmobException e) {
                        if (e == null) {
                            limit = limit + 20;
                            skip = skip + 20;
                            for (JokeData jokeData : list) {
                                if (TextUtils.isEmpty(jokeData.getPicture())) {
                                    jokeList.add(jokeData);
                                }
                            }
                            recyclerView.loadMoreComplete();
                            textJokeAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(getActivity(), e.getMessage());
                        }
                    }
                });
            }
        });

        recyclerView.setRefreshing(true);

        jokeList = new ArrayList<>();
        textJokeAdapter = new TextJokeAdapter(getActivity(), jokeList);
        recyclerView.setAdapter(textJokeAdapter);
        textJokeAdapter.OnItemClick(new TextJokeAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                JokeData joke = jokeList.get(position);
                Intent intent = new Intent(getActivity(), JokeCommentActivity.class);
                intent.putExtra("content", joke.getContent());
                intent.putExtra("hashId", joke.getHashId());
                intent.putExtra("joke", "joke");
                Bundle bundle = new Bundle();
                bundle.putSerializable("jokeData", joke);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserInfoEvent(UserInfoEvent event) {
        textJokeAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserAuthEvent(UserAuthEvent event) {
        textJokeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
